package ai.peanut.selfbms.controller.sys;


import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.enums.ResultTypeEnum;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.office.dao.OfficeMapper;
import ai.peanut.selfbase.modules.office.entity.Office;
import ai.peanut.selfbase.modules.office.pojo.OfficeDto;
import ai.peanut.selfbase.modules.office.pojo.OfficeParams;
import ai.peanut.selfbase.modules.office.service.IOfficeService;
import ai.peanut.selfbase.modules.sys.pojo.ZTreeDto;
import ai.peanut.selfbase.modules.user.entity.User;
import ai.peanut.selfbase.modules.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Controller
@Slf4j
@RequestMapping("/office")
public class OfficeController {
@Autowired
private IUserService iUserService ;
    @Autowired
    private IOfficeService iOfficeService ;

    @RequiresPermissions("office:selectOfficeZTreeListChooseOfficeId")
    @RequestMapping(value = "/selectOfficeZTreeListChooseOfficeId", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<List<ZTreeDto>> getMenuTreeListByRoleId(Long userId , HttpServletRequest request, HttpServletResponse response){
        try {
            User user = iUserService.selectById(userId) ;
            Long officeId = null ;
            List<ZTreeDto> list = new ArrayList<>() ;
            if( userId != null && user == null ) {
                DataResult.success( list ,(Class<List<ZTreeDto>>)list.getClass()) ;
            }
           list = iOfficeService.selectOfficeZTreeListChooseOfficeId(officeId);

            return  DataResult.success( list ,(Class<List<ZTreeDto>>)list.getClass()) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }



    @RequiresPermissions("office:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Office> list(HttpServletRequest request, HttpServletResponse response){
        try {
            OfficeParams params = new OfficeParams() ;
            List<Office> list = iOfficeService.selectAllByParams(params) ;
            return  list ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return null ;
    }

    @RequiresPermissions("office:info")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<OfficeDto> info(Long id ,HttpServletRequest request, HttpServletResponse response){
        try {
            Office office  = iOfficeService.selectById(id) ;
            OfficeDto dto = new OfficeDto() ;
            BeanUtils.copyProperties( office , dto ) ;
            if(office != null && office.getParentId().intValue() != 0 ) {
                Office m = iOfficeService.selectById(office.getParentId()) ;
                if( m != null ) {
                    dto.setParentName(m.getName()) ;
                }
            }
            return  DataResult.success(dto , OfficeDto.class) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("office:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataResult<Boolean> saveOrUpdate(@RequestBody OfficeParams params, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (params.getId() == null) {
                return iOfficeService.insertOffice(params);
            } else {
                return iOfficeService.updateOffice(params);
            }
        } catch (Exception e) {
            log.error("cps商品修改 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("office:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public DataResult<Boolean> delete(@RequestBody OfficeParams params) {
        try {
            log.info("cps商品删除 管理后台 :{}", "请求参数:" + params.toString());
            if( params.getId() == null ){
                return DataResult.faild(ResultTypeEnum.ID_NULL.getCode(),
                        ResultTypeEnum.ID_NULL.getMessage());
            }
            params.setUpdateUserId(UserUtils.getCurrentUser().getId());
            params.setDelFlag(0);
            int deNum = iOfficeService.deleteCurrentAndChildren(params);
            return DataResult.success();
        } catch (Exception e) {
            log.error("cps商品删除 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());

    }


}

